/**
 * 用户详情对话框
 */
var UserInfoDlg = {
    data: {
        deptId: "",
        deptName: ""
    }
};

var TypesInfoDlg = {
    data: {
        typesId: "",
        typesName: ""
    }
};

layui.use(['layer', 'form', 'admin', 'laydate', 'ax','upload','layedit'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var layer = layui.layer;
    var upload = layui.upload;
    var layedit = layui.layedit;
    // 让当前iframe弹层高度适应
    admin.iframeAuto();

    // 点击分类时
    $('#typesName').click(function () {
        var formName = encodeURIComponent("parent.TypesInfoDlg.data.typesName");
        var formId = encodeURIComponent("parent.TypesInfoDlg.data.typesId");
        var treeUrl = encodeURIComponent(Feng.ctxPath + "/types/tree");

        layer.open({
            type: 2,
            title: '分类选择',
            area: ['300px', '400px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                console.log(TypesInfoDlg.data);
                $("#typesId").val(TypesInfoDlg.data.typesId);
                $("#typesName").val(TypesInfoDlg.data.typesName);
            }
        });
    });

    form.on('select(brandId)',function (data){
        var e = data.elem;
        //获取选中的值
        console.log(data.value);
        //获取选中的文本
        var text =e[e.selectedIndex].text;
        $("#brandName").val(text);
        console.log(text);
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var val = $("#id").val();
        var text = $("#id").text();
        console.log(val + ";" + text);
        var ajax = new $ax(Feng.ctxPath + "/drug/add", function (data) {
            Feng.success("添加成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
    });


    var uploadInst = upload.render({
        elem: '#socialImage',
        url: Feng.ctxPath + '/mgr/upload', // 上传接口
        number: 1,
        exts: 'jpg|jpeg|png|',
        before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
            console.log(obj);
            obj.preview(function(index, file, result){
                console.log(index);
                console.log("图片路径："+result);
                $('#preShow').attr('src', result); //图片链接（base64）
            });
        },
        done: function(res){
            console.log("上传结束：" + res.path);
            $('#img_url').attr('value',res.path);
        },
        error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #ff5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });
});